package com.pay.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhouyihe
 * @ClassName RabbitMQConfig
 * @qq 1552951165
 */
@Configuration
public class RabbitMQBatchConfig {
    /**
     * 定义交换机的名batch_pay_ex
     */
    public static final String EXCHANGE_BATCH_NAME = "batch_pay_ex";
    
    /**
     * 微信模板队列
     */
    public static final String BATCH_WECHAT_TEMPLATE_QUEUE = "batch_fanout_wechatTemplate_queue";
    
    /**
     * 缓冲区2条msg
     */
    public static final Integer BUFFER_SIZE = 2;
    
    /**
     * 间隔同步时间
     */
    public static final Long BUFFER_INTERVAL_TIME = 1000l;
    
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        return rabbitTemplate;
    }
    
    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        rabbitAdmin.setAutoStartup(true);
        return rabbitAdmin;
    }
    
    @Bean
    public Queue directQueue() {
        return new Queue(BATCH_WECHAT_TEMPLATE_QUEUE);
    }
    
    //Direct交换机
    @Bean
    FanoutExchange directExchange() {
        return new FanoutExchange(EXCHANGE_BATCH_NAME);
    }
    
    //绑定
    @Bean
    Binding bindingDirect() {
        return BindingBuilder.bind(directQueue()).to(directExchange());
    }
}
